package com.prp.gwt.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.prp.bo.MailBO;
import com.prp.db.po.Mail;
import com.prp.db.po.Mailbox;
import com.prp.db.po.UserInformation;
import com.prp.gwt.client.ClientConstants;
import com.prp.gwt.client.ClientException;
import com.prp.gwt.client.user.mail.MailService;
import com.prp.gwt.client.vo.MailBoxList;
import com.prp.gwt.client.vo.MailInfo;
import com.prp.gwt.client.vo.MailboxInfo;
import com.prp.gwt.client.vo.NewMail;
import com.prp.gwt.client.vo.User;

public class MailServiceImpl extends GlobalServiceImpl implements MailService{

	private static final long serialVersionUID = 7248114071470693340L;

	@Override
	public boolean verify() {
		//return true;
		return super.verify(ClientConstants.USER);
	}
	
	@Override
	public com.prp.gwt.client.vo.User updateUser() throws ClientException {
		validateAuth(ClientConstants.USER);
		return super.updateUser(-1);
	}
	
	@Override
	public MailBoxList getInMailList( ) throws ClientException {
		Integer userId = (Integer) getThreadLocalRequest().getSession()
			.getAttribute(ClientConstants.ATTR_USER);
		return getInboxMailList(userId.intValue());
	}
	
	@Override
	public MailBoxList getOutMailList( ) throws ClientException {
		Integer userId = (Integer) getThreadLocalRequest().getSession()
		.getAttribute(ClientConstants.ATTR_USER);
		return getOutboxMailList(userId.intValue());
	}
	
	@Override
	public MailInfo getMail(int mailboxId)throws ClientException{
		return getMailInfo(mailboxId);
	}

	@Override
	public MailboxInfo deleteInbox(MailboxInfo mailboxInfo)
			throws ClientException {
		MailBO.deleteInInbox(mailboxInfo.getSender().getId(), 
				mailboxInfo.getReceiver().getId(), 
				mailboxInfo.getMailId(), mailboxInfo.getId());
		return mailboxInfo;
	}

	public static void main(String[] args){
		MailServiceImpl service = new MailServiceImpl();
		//MailBO.deleteInOutbox(0, 0, 10, 8);
		//MailBO.deleteInInbox(5,5,10,8);
		MailBO.sendMail(5, 5, 5, "titi" , true);
//		NewMail newMail = new NewMail();
//		newMail.setAddress("5");
//		newMail.setContent("test content  5 5 11");
//		newMail.setReceiverID(5);
//		newMail.setTitle("test mail1");
//		Mail newMailre = MailBO.addMail(newMail.getContent());
//		Mailbox mailbox = MailBO.sendMail(5, newMail.getReceiverID(), newMailre.getId(), newMail.getTitle() , true);
	}
	
	@Override
	public MailboxInfo deleteOutbox(MailboxInfo mailboxInfo)
			throws ClientException {
		MailBO.deleteInOutbox(mailboxInfo.getSender().getIdentity(), 
				mailboxInfo.getReceiver().getIdentity(), 
				mailboxInfo.getMailId(), mailboxInfo.getId());
		return mailboxInfo;
	}
	
	/**
	 * -1 - 发送失败
	 * 0 - 无此用户
	 * 1 - 发送成功
	 */
	public int sendMail(NewMail mail){
		if(!com.prp.bo.User.hasUser(mail.getReceiverID()))
			return 0;
		
		Mail newMail = MailBO.addMail(mail.getContent());	
		Integer userId = (Integer) getThreadLocalRequest().getSession()
			.getAttribute(ClientConstants.ATTR_USER);
		
		Mailbox mailbox = MailBO.sendMail(userId, mail.getReceiverID(), newMail.getId(), mail.getTitle() , true);
		if(mailbox == null){
			return -1;
		}
		return 1;
	}
	
	public MailBoxList getInboxMailList(int userId){
		MailBoxList mbList = new MailBoxList();
		ArrayList<com.prp.db.po.Mailbox> tempMailList = MailBO.getInboxMailList(userId);
		for (Iterator<com.prp.db.po.Mailbox> it = tempMailList.iterator(); it.hasNext();)
			mbList.addMail(getMailboxInfo(it.next().getId()));
		return mbList;
	}
	
	public MailBoxList getOutboxMailList(int userId){
		MailBoxList mbList = new MailBoxList();
		ArrayList<com.prp.db.po.Mailbox> tempMailList = MailBO.getOutboxMailList(userId);
		for (Iterator<com.prp.db.po.Mailbox> it = tempMailList.iterator(); it.hasNext();)
			mbList.addMail(getMailboxInfo(it.next().getId()));
		return mbList;
	}
	
	public MailboxInfo getMailboxInfo(int mailboxId){
		MailboxInfo result = new MailboxInfo();
		Mailbox temp = MailBO.getMailbox(mailboxId);
		UserInformation tempSender = com.prp.bo.User.getUser(temp.getSenderId());
		UserInformation tempReceiver = com.prp.bo.User.getUser(temp.getReceiverId());
		
		User sender = com.prp.bo.User.parse(tempSender);
//		sender.setId(tempSender.getUserId());
//		sender.setAge(tempSender.getAge());
//		sender.setEmail(tempSender.getEmail());
//		sender.setGender(tempSender.getGender());
//		sender.setIdentity(tempSender.getIdentity());
//		sender.setPortrait(tempSender.getPictureRoutine());
//		sender.setRealname(tempSender.getUserRealName());
//		sender.setUsername(tempSender.getUserRegName());
		
		User receiver = com.prp.bo.User.parse(tempReceiver);
//		receiver.setId(tempReceiver.getUserId());
//		receiver.setAge(tempReceiver.getAge());
//		receiver.setEmail(tempReceiver.getEmail());
//		receiver.setGender(tempReceiver.getGender());
//		receiver.setIdentity(tempReceiver.getIdentity());
//		receiver.setPortrait(tempReceiver.getPictureRoutine());
//		receiver.setRealname(tempReceiver.getUserRealName());
//		receiver.setUsername(tempReceiver.getUserRegName());
		
		result.setId(mailboxId);
		result.setMailId(temp.getMailId());
		result.setTitle(temp.getTitle());
		result.setReceiver(receiver);
		result.setSender(sender);
		result.setState(temp.getState());
		
		return result;
	}
	
	public MailInfo getMailInfo(int mailboxId){
		MailboxInfo mailbox = getMailboxInfo(mailboxId);
		Mail mail = MailBO.getMail(mailbox.getMailId());
		MailInfo result = new MailInfo();
		result.setContent(mail.getContent());
		result.setDate(mail.getCreateTime());
		result.setMailboxInfo(mailbox);
		return  result;
	}

	@Override
	public boolean canSendMassMail() {
		
		Object id = getThreadLocalRequest().getSession().getAttribute(ClientConstants.ATTR_USER);
		if (null == id)
			return false;
		if (com.prp.bo.User.getUser((Integer)id).getIdentity() < ClientConstants.ADMIN)
			return false;
		
		return true;
	}

	/**
	 * -1  未完全发送
	 * 1   成功发送
	 * 0   无法获取用户列表
	 */
	@Override
	public int sendMassMail(NewMail mail) {
		int result = 1;
		Mail newMail = MailBO.addMail(mail.getContent());	
//		Integer userId = (Integer) getThreadLocalRequest().getSession()
//			.getAttribute(ClientConstants.ATTR_USER);
		
		List userList = com.prp.bo.User.getUserList();
		//改成遍历之后发送
		if(userList ==  null){
			result = 0;
			return result;
		}
		for(int i = 0; i < userList.size();i++){
			Mailbox mailbox = MailBO.sendMail(0, ((UserInformation)(userList.get(i))).getUserId(), newMail.getId(), mail.getTitle() , true);
			//-1代表此邮件为群发邮件
			if(mailbox == null){
				result = -1;
			}
		}		
		return result;
	}
	
}
